package com.yic.module.wms.controller.admin.warehouse;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.enums.CommonStatusEnum;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.warehouse.vo.*;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.module.wms.convert.warehouse.WarehouseConvert;
import com.yic.module.wms.service.warehouse.WarehouseService;

@Tag(name = "管理后台 - 仓库")
@RestController
@RequestMapping("/wms/warehouse")
@Validated
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;
    @Resource
    private FactoryApi factoryApi;

    @PostMapping("/create")
    @Operation(summary = "创建仓库")
    @PreAuthorize("@ss.hasPermission('wms:warehouse:create')")
    public CommonResult<Long> createWarehouse(@Valid @RequestBody WarehouseCreateReqVO createReqVO) {
        return success(warehouseService.createWarehouse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库")
    @PreAuthorize("@ss.hasPermission('wms:warehouse:update')")
    public CommonResult<Boolean> updateWarehouse(@Valid @RequestBody WarehouseUpdateReqVO updateReqVO) {
        warehouseService.updateWarehouse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:warehouse:delete')")
    public CommonResult<Boolean> deleteWarehouse(@RequestParam("id") Long id) {
        warehouseService.deleteWarehouse(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:warehouse:query')")
    public CommonResult<WarehouseRespVO> getWarehouse(@RequestParam("id") Long id) {
        WarehouseDO warehouse = warehouseService.getWarehouse(id);
        return success(WarehouseConvert.INSTANCE.convert(warehouse));
    }

    @GetMapping("/list")
    @Operation(summary = "获得仓库列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:warehouse:query')")
    public CommonResult<List<WarehouseRespVO>> getWarehouseList(@RequestParam("ids") Collection<Long> ids) {
        List<WarehouseDO> list = warehouseService.getWarehouseList(ids);
        return success(WarehouseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库分页")
    @PreAuthorize("@ss.hasPermission('wms:warehouse:query')")
    public CommonResult<PageResult<WarehouseRespVO>> getWarehousePage(@Valid WarehousePageReqVO pageVO) {
        PageResult<WarehouseDO> pageResult = warehouseService.getWarehousePage(pageVO);
        PageResult<WarehouseRespVO> respVo = WarehouseConvert.INSTANCE.convertPage(pageResult);
        respVo.getList().forEach(resp -> {
            // 封装工厂
            FactoryRespDTO factory = factoryApi.getFactory(resp.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                resp.setFactoryName(factory.getName());
            }
        });
        return success(respVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库 Excel")
    @PreAuthorize("@ss.hasPermission('wms:warehouse:export')")
    @OperateLog(type = EXPORT)
    public void exportWarehouseExcel(@Valid WarehouseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WarehouseDO> list = warehouseService.getWarehouseList(exportReqVO);
        // 导出 Excel
        List<WarehouseExcelVO> datas = WarehouseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "仓库.xls", "数据", WarehouseExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取仓库精简信息列表", description = "只包含被开启的仓库，主要用于前端的下拉选项")
    public CommonResult<List<WarehouseSimpleRespVO>> getSimpleWarehouseList() {
        // 获得仓库列表，只要开启状态的
        List<WarehouseDO> list = warehouseService.getWarehouseList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(WarehouseDO::getSort));
        List<WarehouseSimpleRespVO> warehouseSimpleRespVOS = WarehouseConvert.INSTANCE.convertList03(list);
        warehouseSimpleRespVOS.forEach(warehouseSimpleRespVO -> {
            warehouseSimpleRespVO.setIndex("0-" + warehouseSimpleRespVO.getId());
        });
        return success(warehouseSimpleRespVOS);
    }

}
